package cc.laop.java.concurrent;

/**
 * @Auther: Pengpeng
 * @Date: create in 2018/1/11 17:30
 * @Description:
 */
public class ThreadCommunication {

    private Entity entity = new Entity();

    public static class Entity {
        String message;
    }


    public void threadA() {
        new Thread(() -> {
            synchronized (entity) {
                try {
                    entity.wait();
                    System.out.println("ThreadA message: " + entity.message);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    public void threadB() {
        new Thread(() -> {
            synchronized (entity) {
                entity.message = "Hello world";
                entity.notify();
            }
        }).start();
    }


    public static void main(String[] args) throws InterruptedException {
        ThreadCommunication tc = new ThreadCommunication();
        tc.threadA();
        tc.threadB();
        Thread.sleep(5000);
    }


}
